package com.adinnet.api.schoolWebsite.service.impl;

import com.adinnet.api.address.mapper.AddressMapper;
import com.adinnet.api.address.model.Address;
import com.adinnet.api.address.service.AddressService;
import com.adinnet.api.schoolWebsite.model.SchoolWebsite;
import com.adinnet.api.schoolWebsite.mapper.SchoolWebsiteMapper;
import com.adinnet.api.schoolWebsite.service.SchoolWebsiteService;
import com.adinnet.api.schoolWebsiteImg.mapper.SchoolWebsiteImgMapper;
import com.adinnet.api.schoolWebsiteImg.model.SchoolWebsiteImg;
import com.adinnet.core.redis.RedisConstant;
import com.adinnet.core.redis.RedisService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 校区网点 服务实现类
 * </p>
 *
 * @author zYshuai
 * @since 2020-12-04
 */
@Service
public class SchoolWebsiteServiceImpl extends ServiceImpl<SchoolWebsiteMapper, SchoolWebsite> implements SchoolWebsiteService {


    @Autowired
    private SchoolWebsiteMapper schoolWebsiteMapper;

    @Autowired
    private SchoolWebsiteImgMapper schoolWebsiteImgMapper;

    @Autowired
    private RedisService redisService;

    @Autowired
    private AddressMapper addressMapper;
    /**
     * 分校区列表
     *
     * @param limit
     * @param page
     * @param searchFields
     * @return
     */
    @Override
    public IPage<SchoolWebsite> schoolList(Integer limit, Integer page, String searchFields) {
        QueryWrapper<SchoolWebsite> wrapper = new QueryWrapper<>();
        wrapper.eq("deleted", 0);
        if (StringUtils.isNotEmpty(searchFields)) {
            wrapper.and(infoWrapper -> infoWrapper.like("school_name", searchFields).or()
                    .eq("province_id", searchFields).or().eq("city_id", searchFields).or().eq("area_id", searchFields));
        }
        return schoolWebsiteMapper.selectPage(new Page<>(page, limit), wrapper);
    }

    /**
     * 分校区网点详情
     * @param id
     * @return
     */
    @Override
    public List<String> schoolDetails(Integer id) {
        QueryWrapper<SchoolWebsiteImg> wrapper =new QueryWrapper<>();
        wrapper.eq("deleted",0).eq("school_website_id",id);
        List<SchoolWebsiteImg> schoolWebsiteImgs = schoolWebsiteImgMapper.selectList(wrapper);
        if(CollectionUtils.isEmpty(schoolWebsiteImgs)){
            return null;
        }
        return schoolWebsiteImgs.stream().filter(schoolWebsiteImg -> StringUtils.isNotEmpty(schoolWebsiteImg.getImgUrl())).map(SchoolWebsiteImg::getImgUrl).collect(Collectors.toList());
    }

    /**
     * @Description: 分校区网点 省市区列表
     * @author zYshuai
     * @Date: 2021/1/8 14:16
     * @return: java.util.List<com.adinnet.api.address.model.Address>
     */
    @Override
    public List<Address> schoolAddressList() {
        List<Address> addressList = Lists.newArrayList();
        //查询所有分校区
        List<SchoolWebsite> schoolWebsites = schoolWebsiteMapper.selectList(new QueryWrapper<SchoolWebsite>().eq("deleted", 0));
        if (CollectionUtils.isEmpty(schoolWebsites)) {
            return addressList;
        }
        List<Integer> provinceIds = schoolWebsites.stream().filter(schoolWebsite -> schoolWebsite.getProvinceId() != null).map(SchoolWebsite::getProvinceId).collect(Collectors.toList());
        List<Integer> cityIds = schoolWebsites.stream().filter(schoolWebsite -> schoolWebsite.getCityId() != null).map(SchoolWebsite::getCityId).collect(Collectors.toList());
        List<Integer> areaIds = schoolWebsites.stream().filter(schoolWebsite -> schoolWebsite.getAreaId() != null).map(SchoolWebsite::getAreaId).collect(Collectors.toList());
        //省
        List<Address> provinceList = addressMapper.selectList(new QueryWrapper<Address>().eq("type", 1).eq("deleted", 0).in("id", provinceIds));
        if (CollectionUtils.isNotEmpty(provinceList)) {
            for (Address address : provinceList) {
                List<Address> cityList = Lists.newArrayList();
                if (CollectionUtils.isNotEmpty(cityIds)) {
                    //市
                    cityList = addressMapper.selectList(new QueryWrapper<Address>()
                            .eq("type", 2).eq("deleted", 0).eq("province_code", address.getCode()).in("id", cityIds));
                }
                address.setAddresses(cityList);
                for (Address cityAddress : cityList) {
                    List<Address> areaList = Lists.newArrayList();
                    if (CollectionUtils.isNotEmpty(areaIds)) {
                        //区
                        areaList = addressMapper.selectList(new QueryWrapper<Address>()
                                .eq("type", 3).eq("deleted", 0).eq("city_code", cityAddress.getCode()).in("id", areaIds));
                    }
                    cityAddress.setAddresses(areaList);
                }
                addressList.add(address);
            }
        }
        return addressList;
    }
}
